package com.didi.gulimall.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.didi.gulimall.common.constant.MemberConstant;
import com.didi.gulimall.common.to.OrderBackVo;
import com.didi.gulimall.common.utils.PageUtils;
import com.didi.gulimall.common.utils.Query;
import com.didi.gulimall.member.dao.MemberChangeHistoryDao;
import com.didi.gulimall.member.entity.MemberChangeHistoryEntity;
import com.didi.gulimall.member.service.MemberChangeHistoryService;
import com.didi.gulimall.member.service.MemberService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author 10626
 */
@Service
public class MemberChangeHistoryServiceImpl extends ServiceImpl<MemberChangeHistoryDao, MemberChangeHistoryEntity> implements MemberChangeHistoryService {
    @Autowired
    private MemberService memberService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberChangeHistoryEntity> page = this.page(
                new Query<MemberChangeHistoryEntity>().getPage(params),
                new QueryWrapper<MemberChangeHistoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void deleteShow(List<Long> orderTaskEntity) {
        baseMapper.deleteShow(orderTaskEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void orderRollback(OrderBackVo orderBackVo) {
        QueryWrapper<MemberChangeHistoryEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_sn", orderBackVo.getOrderSn())
                .eq("look_status", MemberConstant.LOCKING);
        List<MemberChangeHistoryEntity> orderTaskEntities = baseMapper.selectList(queryWrapper);

        if (CollectionUtils.isNotEmpty(orderTaskEntities)) {
            Integer integration = 0;
            Integer growth = 0;
            Long memberId = null;
            for (MemberChangeHistoryEntity memberChangeHistoryEntity : orderTaskEntities) {
                memberId = memberChangeHistoryEntity.getMemberId();
                if (memberChangeHistoryEntity.getType().equals(MemberConstant.GROWTH_VALUE)) {
                    growth = Integer.parseInt(memberChangeHistoryEntity.getChangeCount()) * -1;
                }
                if (memberChangeHistoryEntity.getType().equals(MemberConstant.INTEGRATION)) {
                    integration = Integer.parseInt(memberChangeHistoryEntity.getChangeCount()) * -1;
                }
                memberChangeHistoryEntity.setIsDelete(MemberConstant.DELETE);
                memberChangeHistoryEntity.setLookStatus(MemberConstant.UNLOCK);
                memberChangeHistoryEntity.setUnlockTime(new Date());
            }
            memberService.addMemberGI(memberId, growth, integration);
            updateBatchById(orderTaskEntities);
            // TODO 发消息 第三方已经退了 查询登录
        }
    }
}